/****************************************************
          File Name:utf8tohex.c
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Tue 17 Feb 2015 04:24:34 PM CST
*****************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>

int trans(char ch, int op)
{
    int tmp = 0;
    //char tch = ch & 0x0000;
    int char_size = 0;

    char_size = 1 << CHAR_BIT;

    //for(; ch != tch; ch--)
    //    tmp++;
    tmp = ch > 0 ? ch : char_size + ch;
    if(op == 1)
        printf("%X", tmp);
    return tmp;
}

void tobin(char ch)
{
    int tmp;

    tmp = trans(ch, 0);
    for(int i=1; tmp > 0; i++)
    {
        tmp >>= 1;
        if((tmp & 1) == 1)
            printf("%d",1);
        else
            printf("%d", 0);
        if(i % 4 == 0)
            printf(" ");
    }
    printf("\n");
}

int main(int argc, char *argv[])
{
    char *in;
    int len = 0;
    int res = 0;

    in = (char *)malloc(strlen(argv[1]));
    strcpy(in, argv[1]);

    for(int i=0; in[i] !='\0'; i++)
        trans(in[i], 1);
    printf("\n");
    for(int i=0; in[i] !='\0'; i++)
    {
        printf("in[%d] = %d\t0x%X\t", i, in[i], in[i]);
        tobin(in[i]);
    }

    free(in);
    return 0;
}
